function out=get_wrapp_periodization(window,img_size,point)
% periodises the window to the image
%
% author: Sebastian Schmelcher        Version: 2012-06-19


ys=img_size(1);
xs=img_size(2);

window_size=size(window);
yw=window_size(1);
xw=window_size(2);

origin=get_origin(img_size);
yo=origin(1)-floor(yw/2);
xo=origin(2)-floor(xw/2);

ystart=yo-ceil(yo/yw)*yw;
xstart=xo-ceil(xo/xw)*xw;

if(nargin>2)   
    
    %find the left upper wrapp-window from point
    while(ystart+yw<point(1))
       ystart=ystart+yw;
   end
   while(xstart+xw<point(3))
       xstart=xstart+xw;
   end
   
   ystart=ystart-point(1)+1;
   xstart=xstart-point(3)+1;
   
   %size of supp-window
   ys=point(2)-point(1)+1;
   xs=point(4)-point(3)+1;
   

end

out=zeros(ys,xs);

xnow=xstart;
ynow=ystart;

while(ynow<ys)
    while(xnow<xs)
        %window is added, boundaries are taken care of
        out(max(ynow,1):min(ynow+yw-1,ys),max(xnow,1):min(xnow+xw-1,xs))=window(max(-ynow+2,1):min(yw,yw+ys-ynow-yw+1),max(-xnow+2,1):min(xw,xw+xs-xnow-xw+1));     
        xnow=xnow+xw;
    end
    xnow=xstart;
    ynow=ynow+yw;
end